﻿function PrefixInteger(num, length) {
    return (Array(length).join('0') + num).slice(-length);
}

$(function () {
    SetBottomBarPos();
    window.onresize = function () {
        SetBottomBarPos();
    };

    // world page
    $(".world-info").each(function (index, element) {
        if (!$(element).hasClass("world-active")) {
            $(element).hide();
        }
    });

    var world = $(".world-pic");
    world.each(function () {
        var picid = $(this).attr("id");
        var infoid = '#info-' + picid;
        $(this).click(function () {
            $(".world-info").fadeOut();
            $(infoid).fadeIn();
            var newSrc = "images/world/" + picid + "_day.jpg" + '?' + new Date().getTime();
            $.backstretch(newSrc, { speed: 400 });
        });
        var weather = $(infoid + " .weather img");
        weather.each(function () {
            var weatherid = $(this).attr("class").replace(" opacity-button", "");
            $(this).click(function () {
                var newSrc = "images/world/" + picid + "_" + weatherid + ".jpg" + '?' + new Date().getTime();
                $.backstretch(newSrc, { speed: 400 });
            });
        });
    });

    // Character page
    $(".character-info .chara-info").each(function (index, element) {
        if (!$(element).hasClass("chara-active")) {
            $(element).hide();
        }
    });

    $(".character-info .chara-info .voice").each(function (index, element) {
        var section = $(this).parent();
        var charId = section.attr("id");
        // console.info(section, charId);
        var voices = $(this).find(".voice-button");
        // console.info(voices);
        voices.each(function (index, element) {
            var voicePath = "voice/" + charId + PrefixInteger((index + 1), 2);
            // console.info(voicePath);
            var bgm = $("<div></div>");
            bgm.jPlayer({
                ready: function () {
                    $(this).jPlayer("setMedia", {
                        oga: voicePath + ".ogg",
                        mp3: voicePath + ".mp3"
                    }).jPlayer('stop');
                },
                volume: 0.5,
                ended: function (event) {
                    // console.info("ended");
                },
                preload: 'auto',
                //swfPath: "/" + thisTitle + "/js",
                supplied: "oga,mp3",
                solution: "html, flash"
            });
            section.append(bgm);
            $(this).click(function () {
                // console.info("play: " + bgm.attr("id"));
                bgm.jPlayer('play');
            });
        });
    });

    var headBtns = $(".head a");
    headBtns.each(function () {
        var picid = $(this).attr("id").replace("head_", "");
        $(this).click(function () {
            $("." + "lihui_active").animate({ left: '10px', opacity: '0.0' }, "500");
            $("." + "chara-active").animate({ top: '10px', opacity: '0.0' }, "500", function () {
                $("." + "lihui_active").css("display", "none");
                $("." + "lihui_active").removeClass("lihui_active");
                $("#lihui_" + picid).addClass("lihui_active");
                $("#lihui_" + picid).css("display", "block");
                $("#lihui_" + picid).animate({ opacity: '1.0' }, "500");

                $("." + "chara-active").css("display", "none");
                $("." + "chara-active").removeClass("chara-active");
                $("#" + picid).addClass("chara-active");
                $("#" + picid).css("display", "block");
                $("#" + picid).animate({ top: '-10px', opacity: '1.0' }, "500");
            });
        });
    });

    var styleBtns = $(".chara-style img");
    styleBtns.each(function () {
        var picid = $(this).attr("src").replace("images/style_", "");
        picid = picid.replace(".png", "");
        picid = "lihui_" + picid;
        $(this).click(function () {
            $("." + "lihui_active").animate({ opacity: '0.0' }, "500", function () {
                $("." + "lihui_active").css("display", "none");
                $("." + "lihui_active").removeClass("lihui_active");
                $("#" + picid).addClass("lihui_active");
                $("#" + picid).css("display", "block");
                $("#" + picid).animate({ opacity: '1.0' }, "500");
            });
        });
    });
});